
// 路由模块
const getRouter = require('router');
const router = getRouter();

// 引入集合模块
const Student = require('../model/user');

// 配置模板相关文件
const template = require('art-template');

const queryString = require('querystring');


// get
router.get(['/add', '/'], (req, res) => {
    let html = template('index', {
    })
    res.end(html);
});

router.get('/list', async (req, res) => {
    let users = await Student.find()
    let html = template('list', {
        users
    })
    res.end(html);
});

// post
router.post('/add', (req, res) => {
    let result = '';
    req.on('data', params => {
        result += params;
    });
    req.on('end', async params => {
        let formData = queryString.parse(result);
        await Student.create(formData);
    });
    res.writeHead(301, {
        Location: '/list'
    });
    res.end();
});

module.exports = router;